import json

def get_params(json_file,target_desc,filter_prefix=None,select_keys=None):
    """
    提取json文件中数据的方法
    参数：
        json_file: 指定的json文件路径
        target_desc: 要提取的对象的desc值
        filter_prefix: 可选，提取键名为该前缀开头的数据
        select_keys: 可选，指定要提取的键列表
    说明：
        若同时指定filter_prefix和select_keys，则优先使用select_keys
        若两者都没有指定，则提取除desc以外的所有键值对
    :return:
    """
    result_dict = {}
    try:
        with open(json_file,'r',encoding='utf-8') as f:
            data = json.load(f)
        #找到目标decs
        trage_obj = next((item for item in data if item.get('desc')==target_desc), None)
        if not trage_obj:
            print(f"未找到desc为'{target_desc}'的对象")
            return result_dict
        #开始提取
        for key,value in trage_obj.items():
            if key == 'desc':
                continue
            if select_keys and key in select_keys:
                result_dict[key] = value
            elif filter_prefix and key in filter_prefix:
                result_dict[key] = value
            else:
                result_dict[key] = value
        return result_dict

    except FileNotFoundError:
        print(f"错误，文件'{json_file}不存在")
    except json.JSONDecodeError:
        print(f"错误，文件'{json_file}'不是有效的格式")
    except Exception as e:
        print(f"提取数据时出错:{str(e)}")

    return result_dict
